﻿package learnmath.windows.elements{
/*-------------------------------------------------------------
	Created by: Ionel Alexandru 
	Mail: ionel.alexandru@gmail.com
	Site: www.learn-math.info
---------------------------------------------------------------*/
import learnmath.windows.*;
import learnmath.windows.elements.*;
import learnmath.windows.util.*;
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;


public class IconsManager{

	var pannel:MovieClip;
	var pannelNextX = 50;
	var pannelNextY = 30;

	var taskBar:TaskBar;
	var windowsManager:WindowsManager;
	
	var iconList = new Array();

	public function IconsManager(_taskBar:TaskBar, _windowsManager:WindowsManager){
		pannel = AppManager.mainPannel.workingPannel;
		taskBar = _taskBar;
		windowsManager = _windowsManager;
		
		var desktopAction = new DesktopMc();
		taskBar.addIcon(desktopAction);
		desktopAction.executeApp="Desktop";
		desktopAction.addEventListener(MouseEvent.CLICK, desktopExecute);

		var apps:Array = ConfigManager.applications;
		for(var i=0; i<apps.length; i++){
			var appbean:AppBean = apps[i];
			if(appbean.getIconBigMc()==null) continue;
			addIconTo(appbean, _taskBar);		
		}
		for(var i=0; i<iconList.length; i++){
			var iMc = iconList[i];
			iMc.rotationY = i * 180/(iconList.length+1);
		}
		
		taskBar.startButton.pannel.executeApp="Editor";
		taskBar.startButton.pannel.addEventListener(MouseEvent.CLICK, execute);
		
	}
	public function bringIconsInFront():void{
		for(var i=0; i<iconList.length; i++){
			var iMc = iconList[i];
			pannel.removeChild(iMc);
			pannel.addChild(iMc);
		}
	}

	public function addIconTo(appbean:AppBean , taskBar:TaskBar):void{
		var appIcon:MovieClip = new MovieClip();
		appIcon.mouseChildren = false;
		appIcon.mouseEnabled = true;
		appIcon.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
		appIcon.addEventListener(MouseEvent.MOUSE_UP, dropIt);
		appIcon.addEventListener(Event.ENTER_FRAME, rotateX);
		appIcon.addEventListener(MouseEvent.MOUSE_OVER, overIcon);
		appIcon.addEventListener(MouseEvent.MOUSE_OUT, outIcon);
		appIcon.inRotation = true;
		appIcon.buttonMode = true;

		var t:TextField = new TextField();
		t.defaultTextFormat = ConfigManager.getTextFormatIcon();
		t.text = appbean.name;
		t.selectable = false;
		t.x = -35;
		t.y = 50;
		t.width=70;
		t.mouseEnabled = false;

		var	i=appbean.getIconBigMc();
		i.x = - 25;
		appIcon.addChild(i);
		appIcon.addChild(t);
		iconList[iconList.length] = appIcon;
		appIcon.rotationY = iconList.length*15;
	
		pannel.addChild(appIcon);
		appIcon.x = pannelNextX;
		appIcon.y = pannelNextY;
		if((pannelNextY + 75)>(AppManager.mainPannel.height-30)){
			pannelNextY = pannelNextY + 20;
			pannelNextX = pannelNextX + 75;
		}else{
			pannelNextY = pannelNextY + 75;
		}
		appIcon.executeApp=appbean.id;
		appIcon.doubleClickEnabled = true;
		appIcon.addEventListener(MouseEvent.DOUBLE_CLICK, execute);
		
		if(appbean.displayTaskbar){
			var littleIconMc = appbean.getIconLittleMc();
			if(littleIconMc!=null){
				taskBar.addIcon(littleIconMc);
				littleIconMc.executeApp=appbean.id;
				littleIconMc.addEventListener(MouseEvent.CLICK, execute);
			}
		}
	}

	function rotateX(evt:Event):void
	{
		var i = evt.target;
		if(i.inRotation){
			evt.target.rotationY+=5 ;
		}
	}
	function overIcon(event:MouseEvent):void {
		event.currentTarget.inRotation = false;
	}
	function outIcon(event:MouseEvent):void {
		event.currentTarget.inRotation = true;
	}

	function pickUp(event:MouseEvent):void {
		event.currentTarget.rotationY = 0;
		event.currentTarget.startDrag();
	}
	function dropIt(event:MouseEvent):void {
		event.currentTarget.stopDrag();
	}

	public function execute(event:MouseEvent):void {
		var apps:Array = ConfigManager.applications;
		for(var i=0; i<apps.length; i++){
			var appbean:AppBean = apps[i];
			if(event.target.executeApp==appbean.id){
				windowsManager.createWindowForApp(appbean);
				break;
			}
		}
		
	}
	public function desktopExecute(event:MouseEvent):void {
		windowsManager.viewDesktop();
	}
}
}